# Maintainer: Saarko Sandomir <sandomir@tutanota.com>
# Contributor: Saarko Sandomir <sandomir@tutanota.com>
# Contributor: Holger Weiss <holger@zedat.fu-berlin.de>
pkgname=eturnal
pkgver=1.12.0
pkgrel=2
pkgdesc="STUN/TURN server"
url="https://eturnal.net"
arch="all"
license="Apache-2.0 license"
makedepends="
	erlang-dev
	openssl-dev
	yaml-dev
	"
_erldepends="
	accept
	conf
	fast_tls
	fast_yaml
	influx_udp
	p1_utils
	poolboy
	prometheus
	prometheus_httpd
	quantile_estimator
	recon
	stun
	ulitos
	yval
	"
_accept=0.3.5
_conf=0.2.6
_fast_tls=1.1.16
_fast_yaml=1.0.36
_influx_udp=1.1.2
_p1_utils=1.0.25
_poolboy=1.5.2
_prometheus=4.10.0
_prometheus_httpd=2.1.11
_quantile_estimator=0.2.1
_recon=2.5.4
_stun=1.2.9
_ulitos=0.4.0
_yval=1.0.10
_profile="prod"
pkgusers="$pkgname"
pkggroups="$pkgname"
subpackages="$pkgname-doc $pkgname-openrc"
install="$pkgname.pre-install"
source="
	https://eturnal.net/download/eturnal-$pkgver.tar.gz
	https://repo.hex.pm/tarballs/accept-$_accept.tar
	https://repo.hex.pm/tarballs/conf-$_conf.tar
	https://repo.hex.pm/tarballs/fast_tls-$_fast_tls.tar
	https://repo.hex.pm/tarballs/fast_yaml-$_fast_yaml.tar
	https://repo.hex.pm/tarballs/influx_udp-$_influx_udp.tar
	https://repo.hex.pm/tarballs/p1_utils-$_p1_utils.tar
	https://repo.hex.pm/tarballs/poolboy-$_poolboy.tar
	https://repo.hex.pm/tarballs/prometheus-$_prometheus.tar
	https://repo.hex.pm/tarballs/prometheus_httpd-$_prometheus_httpd.tar
	https://repo.hex.pm/tarballs/quantile_estimator-$_quantile_estimator.tar
	https://repo.hex.pm/tarballs/recon-$_recon.tar
	https://repo.hex.pm/tarballs/stun-$_stun.tar
	https://repo.hex.pm/tarballs/ulitos-$_ulitos.tar
	https://repo.hex.pm/tarballs/yval-$_yval.tar
	0-eturnal.yml.patch
	1-eturnalctl.patch
	eturnal.logrotate
	eturnal.initd
	eturnal.confd
	"

prepare() {
	default_prepare
	# each archive retrieved from hex.pm contains a contents.tar.gz, extract
	# that into _build/default/lib/$dep:
	local libdir="$builddir"/_build/default/lib
	for dep in $_erldepends
	do
		local depvsn='' # Make ShellCheck (SC2154) happy.
		eval depvsn='$_'$dep
		local depdir="$libdir"/$dep
		local deptar="$srcdir"/$dep-$depvsn.tar
		mkdir -p "$depdir"
		tar -C "$builddir" -xf "$deptar" contents.tar.gz
		tar -C "$depdir" -xzf "$builddir"/contents.tar.gz
		rm "$builddir"/contents.tar.gz
	done
}

build() {
	export ETURNAL_PREFIX="/usr/lib/eturnal"
	export ERL_EPMD_ADDRESS=""
	./rebar3 as $_profile release
}

check() {
	./rebar3 xref
	./rebar3 eunit
	./rebar3 ct
}

package() {
	# eturnal main package:
	## create application directory
	install -dm755 "$pkgdir"/usr/lib/$pkgname
	## install application files
	install -Dm755 \
		"$builddir"/_build/$_profile/rel/$pkgname/bin/eturnalctl \
		"$pkgdir"/usr/sbin/eturnalctl
	rm "$builddir"/_build/$_profile/rel/$pkgname/bin/eturnalctl
	cp -r \
		"$builddir"/_build/$_profile/rel/$pkgname/bin \
		"$builddir"/_build/$_profile/rel/$pkgname/lib \
		"$builddir"/_build/$_profile/rel/$pkgname/releases \
		"$builddir"/_build/$_profile/rel/$pkgname/erts-* \
		"$pkgdir"/usr/lib/$pkgname
	## install configuration file
	install -Dm640 -o $pkgusers -g $pkggroups \
		"$builddir"/_build/$_profile/rel/$pkgname/etc/$pkgname.yml \
		"$pkgdir"/etc/$pkgname.yml
	## create log directory
	install -dm755 -o $pkgusers -g $pkggroups "$pkgdir"/var/log/$pkgname
	## install init script and logrotate configuration
	install -Dm755 \
		"$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 \
		"$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm644 \
		"$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname
	# eturnal-doc package:
	install -Dm644 \
		"$builddir"/_build/$_profile/rel/$pkgname/doc/LICENSE.txt \
		"$builddir"/_build/$_profile/rel/$pkgname/doc/README.md \
		"$builddir"/_build/$_profile/rel/$pkgname/doc/CHANGELOG.md \
		-t "$pkgdir"/usr/share/doc/$pkgname
}

sha512sums="
b990fd010d26dc29ab69124dd6db354ab018c1b8882ef7f82631c06234b79027ba838a746d6c606a9f6678feffb6bcaa2b879270c3abe407ef509a5f50367774  eturnal-1.12.0.tar.gz
97e42f8f903a87adca02676765cff20eb15ae4807ce63768e9942a379347cc0b574d926e540f60a4c5ff7359e5e850a3d31c03dd69b786dfc5b97200e669462a  accept-0.3.5.tar
25628322f77b2d5f359d4197a4da8578159f26d960fc73781839b91da3206591e2613d34fb91dc0443c32c2135ba9f56ed82b85fc613170f97a78efe931eb90e  conf-0.2.6.tar
d01605ebfa6ebce548cd619d8e67cd057b97bfb9ef8d90ab7f889613c0bb84986a171a7aee1dee1abd73b58bc64622a8dee31bf10ac1c2ffc67aaddc80173e4a  fast_tls-1.1.16.tar
9130d4d7dbc002f55bfe3411e2b9b6d4ab42b642861666ab214cea0a2d90fc75acbd2689a0d46ff8e7f196be73b6bc3b361b4253ddea91520385faa52b3ed75b  fast_yaml-1.0.36.tar
99a039fab1dbaded923ad694d86d56c784b0f5a0457aa743ff8620b0dd820cd196e32fdf6964bac776a77c0917b2d642e0c4295ccd9f1ba353aaf956f2ae8563  influx_udp-1.1.2.tar
c97b67c41483cda98360edea6a4c21c4e01e8a3f0fa6263741775a19172b76fc5e346797740482141003861319d33393ef2808ef6f3e8509d36f03ce65b11817  p1_utils-1.0.25.tar
d44621286277d3a3c4590ad24d4771245bed649ac40c6b7c6d32fb8d161de943b2599fd4a323551c798dbe2a39b4594dd701e1364a676295b73a924b2c03e655  poolboy-1.5.2.tar
ad47d77d1532404fd4e43370be021865a971277c507fc4fd21bd58309511244429c26eb1dbaec3e3c96290c2d27ca860c6011579cbfac8c870615c5c175013e7  prometheus-4.10.0.tar
1c847b1e7496ab95bce8320b5b43ee83c3d4f622d321e1c66fb2e554ad9430ab422270ac73b6853ce9f7dc52517bdf51093de6c99551ccc2e8593e5a42ca4d33  prometheus_httpd-2.1.11.tar
c7845a715d6ee8b4d67f751690c5def7a35bfed7cfe8c147b12dcf4e3a9048e35254a38e7f51b3059c54d4b780498b8e8ef2cec9195e5d0f431006d04a721518  quantile_estimator-0.2.1.tar
a90707d811b836f0e9825617e087ed147fe0d71cb587c6e39fa3588db2cc2920c364a8ba02ed1ed375203310d97fb6fc2e3ad1c2209a655b2bb6a412238fe83a  recon-2.5.4.tar
eeb18ea1e89bf6a629db48b2159f33e65cf54ff47d06f3151576d1b40a1ae8076e031dd559b8fde3edb7644ea6e9f8ec821aa81d5319cc37385e57ff21079014  stun-1.2.9.tar
06f28e7ef6e6505d6adc2240c847ef53f908ab83fd100f022ef8c364aa31c579fe4567994c1bf6f1151c1d25e4738600e74edcce4d8447eebe1f527252a609ca  ulitos-0.4.0.tar
25f815c751ce0dd1d045cdae92d8c5697ac07b88eae1a47b9381e2efc1eabc2c02e4d0a5347fa9ddf8de0f3df9aba04ccc35d54fae4829af655ee6e1183bde7a  yval-1.0.10.tar
d30251616f555d6ccbb47a9bff3ee33eda1c4d4a73dd1e140ff036706eb20a02062cc68e78f3c0a19597e72389a27af592e101d86fcb5f0aaaec30febff57e6e  0-eturnal.yml.patch
cdf20cad2b07acf50a31aafe4ab2cd3b1a1ca2e4feb226296a040466fa801ab5073025c6c16b87651b44bb7ebbba56f4ed020828b4ab5b82bafe75fe4709f403  1-eturnalctl.patch
ac7be5b7bb7be2d51b52b3dadd5976f45e0980b43ac43451043756543cbcf954f54eacc66ada7e5c2f9cf9630755d75065f4eb194ee1f7ad6a2199dcec322f04  eturnal.logrotate
b0dd673fa2165c2d238e1edb18bdeabbed69140bd3d16b50d8bda54131671c482279902c5f7c545c887136a9d26dbb216968e8896f55f08343a2a6d5575578c0  eturnal.initd
29338c6cf5c79c5c4b0cc739f479f42a8af7c654ce4f5f945f973026256666cc152cf049e6caf7ae2e3f9fb456851fa6e29bb2b39fbf33d2cae094e3a05289a5  eturnal.confd
"
